// components/addPanel/addPanel.js
// 引入日期工具
import dateFormat from "../../utils/date"


Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isEdit:{
      type:Boolean,
      value:false
    },
    tabType:{
      type:Number,
      value:0
    },
    addPanelData:{
      type:Object,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 标题
    title: "",
    // 描述信息
    desc: "",
    // 当前日期
    date: {},
    // 当前时间
    time: {},
    // 循环选项
    circulation: "",
    // 持续时间
    duringTime: "",
    // 单选按钮组
    radioItems: [
      {id:0, value: "schedule", name: "待办事项", checked: true },
      {id:1, value: "clock", name: "打卡", checked: false },
      {id:2, value: "target", name: "目标", checked: false },
      {id:3, value: "memorialDay", name: "纪念日", checked: false },
    ],
    circulationItems: [
      {id:0, value: "null", name: "无", checked: true },
      {id:1, value: "daily", name: "每天", checked: false },
      {id:2, value: "weekly", name: "每周", checked: false }
    ],
    duringTimeItems: [
      {id:0, value: "30", name: "30天", checked: true },
      {id:1, value: "60", name: "60天", checked: false },
    ],
    // 是否展示日期控件的Flag
    showDatePanel: false,
    // 是否展示时间控件的Flag
    showTimePanel: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handelTapAddExit(e) {
      const { flag } = e.target.dataset
      if (flag == 'cancel') {
        // 触发父组件回调
        return this.triggerEvent("tapExit", flag)
      } else if (flag == 'save') {
        // 检查是否填写了标题
        if (this.data.title == '') {
          wx.showToast({
            title: '未填写标题！',  // 标题
            icon: 'error',   // 图标类型，默认success
            duration: 1500   // 提示窗停留时间，默认1500ms
          })
          return
        }
        // 构造返回对象
        let result = {}
        // 选择的是-待办事项或打卡
        if (this.data.radioItems[0].checked || this.data.radioItems[1]) {
          result = {}
          result.date = this.data.date
          result.time = this.data.time
          this.data.circulationItems.forEach(item=>{
            if(item.checked){
              result.circulation = item.value
            }
          })
          if (this.data.radioItems[0].checked) {
            result.type = 'schedule'
          } else if (this.data.radioItems[1].checked) {
            result.type = 'clock'
          }
        }
        // 选择的是-目标
        if (this.data.radioItems[2].checked){
          result = {}
          let duringTime
          this.data.duringTimeItems.forEach(item=>{
            if(item.checked){
              duringTime = item.value
            }
          })
          result.type = 'target'
          result.duringTime = duringTime
        }
        // 选择的是-纪念日
        if (this.data.radioItems[3].checked){
          result = {}
          result.type = 'memorialday'
          result.date = this.data.date
        }
        result.title = this.data.title
        result.desc = this.data.desc
        result.flag = flag
        result._id = this.data._id
        // 获取openid
        // result.openid = wx.getStorageSync('openid')
        return this.triggerEvent("tapExit", result)
      }

    },

    // 点击删除
    handelTapDelete(){
      const {_id} = this.data
      wx.showLoading({
        title: '请稍等',
        mask:true
      })
      // 调用云函数进行删除
      wx.cloud.callFunction({
        name:'deleteSchedule',
        data:{
          _id
        },
        success:res=>{
          wx.showToast({
            title: '删除成功',
            mask:true
          })
          console.log('save')
          this.triggerEvent('save')
        }
      })
    },

    // 点击添加事件中的日期按钮
    handelTapDate() {
      // 展示控件
      this.setData({
        showDatePanel: !this.data.showDatePanel
      })
    },

    // 点击添加事件中的时间按钮
    handelTapTime() {
      this.setData({
        showTimePanel: true
      })
    },

    // 子组件触发的exit
    handelTapDateExit(e) {
      const newDate = e.detail
      if (newDate == null) {
        this.setData({ showDatePanel: false })
        return
      }
      // 设值
      this.setData({
        date: newDate,
        showDatePanel: false
      })
    },

    // 子组件触发TimeExit
    handelTapDateTimeExit(e) {
      const newTime = e.detail
      if (newTime == null) {
        this.setData({ showTimePanel: false })
        return
      }
      // 设值
      this.setData({
        time: newTime,
        showTimePanel: false
      })
    },

    // 单选按钮变化
    radioChange: function (e) {
      var items = this.data.radioItems;
      for (var i = 0; i < items.length; i++) {
        items[i].checked = items[i].value == e.detail.value
      }
      this.setData({
        radioItems: items
      });
    },

    // 单选按钮变化
    radioChange2: function (e) {
      var items = this.data.circulationItems;
      for (var i = 0; i < items.length; i++) {
        items[i].checked = items[i].value == e.detail.value
      }
      this.setData({
        circulationItems: items
      });
    },


    // 单选按钮变化
    radioChange3: function (e) {
      var items = this.data.duringTimeItems;
      for (var i = 0; i < items.length; i++) {
        items[i].checked = items[i].value == e.detail.value
      }
      this.setData({
        duringTimeItems: items
      });
    },

    // 输出title事件-获取title值
    handelInputTitle(e) {
      const { value } = e.detail
      this.setData({
        title: value
      })
    },

    // 输入描述信息事件-获取描述信息
    handelInputDesc(e) {
      const { value } = e.detail
      this.setData({
        desc: value
      })
    }
  },

  lifetimes: {
    attached() {
      // 初始化radio选项
      let radioItems = this.data.radioItems
      radioItems.map(item=>{
        if(item.id == this.properties.tabType){
          item.checked = true
        }else{
          item.checked = false
        }
        return item
      })


      // 初始化 addPanel数据
      let addPanelData = this.properties.addPanelData
      this.setData({
        ...addPanelData
      })

      let circulationItems = this.data.circulationItems
      circulationItems.map(item=>{
        if(item.value == addPanelData.circulation){
          item.checked = true
        }else{
          item.checked = false
        }
        return item
      })

      let duringTimeItems = this.data.duringTimeItems
      duringTimeItems.map(item=>{
        if(item.value == addPanelData.duringTime){
          item.checked = true
        }else{
          item.checked = false
        }
        return item
      })

      this.setData({circulationItems,duringTimeItems})
      // this.setData({
      //   date: dateFormat("YYYY-mm-dd", new Date()),
      //   // 五分钟后的时间
      //   time: dateFormat("HH:MM", new Date(new Date().getTime() + 1000 * 60 * 5))
      // })
    }
  }
})
